package com.teaching.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.config.TeachingProperties;
import com.teaching.entity.Attach;
import com.teaching.mapper.AttachMapper;
import com.teaching.service.AttachService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 附件表
 *
 * @author xubb
 * @date 2021-03-22 15:27:55
 */
@Service
public class AttachServiceImpl extends ServiceImpl<AttachMapper, Attach> implements AttachService {


    @Resource
    private TeachingProperties teachingProperties;


    /**
     * 通用保存文件
     *
     * @param file
     * @param groupGUid
     * @param groupType
     * @throws IOException
     */
    @Override
    public String save(MultipartFile file, String groupGUid, String groupType) throws IOException {
        Date now = new Date();
        String time = DateUtil.format(now, "yyyyMM");
        String attachId = StrUtil.uuid();
        String rootPath = teachingProperties.getFileRootPath();
        String filename = file.getOriginalFilename();
        String fullPath = rootPath + File.separator + time + File.separator + groupGUid + File.separator + attachId + File.separator + filename;

        File f = FileUtil.writeFromStream(file.getInputStream(), fullPath);
        String fileType = FileUtil.getType(f);

        Attach attachInfo = new Attach();
        attachInfo.setAttachId(attachId);
        attachInfo.setAttachName(filename);
        attachInfo.setAttachType(fileType);
        attachInfo.setAttachPath(fullPath);
        attachInfo.setGroupGuid(groupGUid);
        attachInfo.setGroupType(groupType);
        attachInfo.setUploadTime(now);

        this.save(attachInfo);
        return attachId;
    }


    /**
     * 导出、下载文件
     *
     * @param file
     * @return
     */
    @Override
    public ResponseEntity<FileSystemResource> export(File file) throws UnsupportedEncodingException {
        if (file == null) {
            return null;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", new Date().toString());
        headers.add("ETag", String.valueOf(System.currentTimeMillis()));
        return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
    }


}
